<?php

require_once('../config.php');
require_once('./messages.php');
require_once('./utility.php');

ignore_user_abort(true);

if(!isset($_GET['protocol']))
{
  SendCorrupt();
}
else
{
  $protocol = $_GET['protocol'];
}

if(isset($_GET['action']))
{
  $action = strtolower($_GET['action']);

  switch($action)
  {
  case MsgRegister:
  case MsgGetSectorCode: 
  case MsgGetSettings: break;

  default:
    if(!isset($_GET['username'], $_GET['password']))
      SendCorrupt();

    $username = strtolower($_GET['username']);
    $password = $_GET['password']; // md5 hashed

    try
    {
      VerifyUser($username, $password);
    }
    catch(Exception $e)
    {
      SendLoginFailed();
    }

    break;
  }

  switch($action)
  {
    case MsgRegister:

      if(!isset($_GET['username'], $_GET['password']))
        SendCorrupt();

      $username = $_GET['username'];
      $password = $_GET['password'];

      try
      {
        AddUser($username, $password);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgLogin:

      Send(array("Sectors"=>array_keys(GetSectors($username)), "Pricelists"=>array_keys(GetPricelists($username)))); // These messages will be requested anyways.

      break;

    case MsgGetSettings:

      Send(array('MaxSectors'    => MAX_SAVED_SECTORS,
                 'MaxPricelists' => MAX_SAVED_PRICELISTS,
                 'Time'          => gmmktime()
                 ));

      break;

    case MsgDeleteUser:

      DeleteUser($username);
      Send();

      break;

    case MsgAddSector:

      if(!isset($_POST['data'], $_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];
      $data = $_POST['data'];

      try
      {
        AddSector($username, $name, $data);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgAddPricelist:

      if(!isset($_POST['data'], $_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];
      $data = $_POST['data'];

      try
      {
        AddPricelist($username, $name, $data);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgUpdateSector:

      if(!isset($_POST['data'], $_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];
      $data = $_POST['data'];

      try
      {
        UpdateSector($username, $name, $data);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgUpdatePricelist:

      if(!isset($_POST['data'], $_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];
      $data = $_POST['data'];

      try
      {
        UpdatePricelist($username, $name, $data);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgRenameSector:

      if(!isset($_POST['oldName'], $_POST['newName']))
        SendCorrupt();

      $oldName = $_POST['oldName'];
      $newName = $_POST['newName'];

      try
      {
        RenameSector($username, $oldName, $newName);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgRenamePricelist:

      if(!isset($_POST['oldName'], $_POST['newName']))
        SendCorrupt();

      $oldName = $_POST['oldName'];
      $newName = $_POST['newName'];

      try
      {
        RenamePricelist($username, $oldName, $newName);
        Send();
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgDeleteSector:

      if(!isset($_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];

      DeleteSector($username, $name);
      Send();

      break;

    case MsgDeletePricelist:

      if(!isset($_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];

      DeletePricelist($username, $name);
      Send();

      break;

    case MsgGetSectors:

      Send(array('Sectors' => array_keys(GetSectors($username))));

      break;

    case MsgGetPricelists:

      Send(array('Pricelists' => array_keys(GetPricelists($username))));

      break;

    case MsgGetSector:

      if(!isset($_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];

      try
      {
        $code = GetSector($username, $name);
        $mapName = ExtractMap($code);
        Send(array('Code' => $code, 'Map' => GetSectorCode($mapName), 'MapName' => $mapName));
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgGetPricelist:

      if(!isset($_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];

      try
      {
        Send(array('Code' => GetPricelist($username, $name)));
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;

    case MsgGetSectorCode:

      if(!isset($_POST['name']))
        SendCorrupt();

      $name = $_POST['name'];

      try
      {
        Send(array('Code' => GetSectorCode($name)));
      }
      catch(Exception $e)
      {
        SendError($e->getMessage());
      }

      break;
  }
}

?>